﻿using Admin.NET.Core;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using IotFun.Const;

using IotFun.Views.MainView.Dto;
using Microsoft.AspNetCore.Mvc;
using IotFun.Entity;
using Mapster;
using Furion.FriendlyException;
using Furion.DatabaseAccessor;
using Microsoft.AspNetCore.Authorization;
namespace IotFun.Views.MainView;
/// <summary>
/// 大屏设计器服务
/// </summary>
[ApiDescriptionSettings(IotConst.GroupName)]
public class MainViewService : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<GenerateMainView> _rep;
    public MainViewService(SqlSugarRepository<GenerateMainView> rep)
    {
        _rep = rep;
    }

    /// <summary>
    /// 分页查询
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Page")]
    public async Task<SqlSugarPagedList<MainViewOutput>> Page([FromQuery] MainViewInput input)
    {
        var query = _rep.AsQueryable()

                    .Select<MainViewOutput>()
;
        query = query.OrderBuilder(input);
        return await query.ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 增加视图
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    public async Task Add(AddMainViewInput input)
    {
        var entity = input.Adapt<GenerateMainView>();
        await _rep.InsertAsync(entity);
    }

    /// <summary>
    /// 增加点位
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [UnitOfWork]
    [ApiDescriptionSettings(Name = "AddTags")]
    public async Task AddTags(AddViewTagInput input)
    {
        var entity = await _rep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);

        entity.ColNum = input.ColNum;
        entity.RowHeight = input.RowHeight;

        await _rep.UpdateAsync(entity);
       await _rep.Context.Deleteable<ViewTag>(x => x.ViewId == input.Id).ExecuteCommandAsync();

        var viewTags = input.Tags.Adapt<List<ViewTag>>();

        viewTags.ForEach(x=> 
        {
            x.ViewId = input.Id;
                     
         }
        );

       await _rep.Context.Insertable<ViewTag>(viewTags).ExecuteCommandAsync();

    }
    /// <summary>
    /// 获取视图点位
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<AddViewTagInput> GetTags([FromQuery] AddViewTagInput input)
    {
      var view=await _rep.GetByIdAsync(input.Id);
      var tags= await _rep.Context.Queryable<ViewTag>().Where(x=>x.ViewId==input.Id).Select<ViewTagInput>().ToListAsync();

        return new AddViewTagInput
        {
            Tags = tags,
            ColNum = view.ColNum,
            RowHeight = view.RowHeight

        };
    }

    /// <summary>
    /// 获取视图点位
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [AllowAnonymous]
    public async Task<AddViewTagInput> GetTagsByName([FromQuery] string name)
    {
        var view = await _rep.Context.Queryable<GenerateMainView>().Where(x => x.Name == name).FirstAsync();
        if (view != null)
        {
            var tags = await _rep.Context.Queryable<ViewTag>().Where(x => x.ViewId == view.Id).Select<ViewTagInput>().ToListAsync();

            return new AddViewTagInput
            {
                Tags = tags,
                ColNum = view.ColNum,
                RowHeight = view.RowHeight

            };
        }
        else
        {
            return new AddViewTagInput();
        }
       
    }

    /// <summary>
    /// 删除视图
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [UnitOfWork]
    [ApiDescriptionSettings(Name = "Delete")]
    public async Task Delete(DeleteMainViewInput input)
    {
        var entity = await _rep.GetFirstAsync(u => u.Id == input.id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);

        await _rep.Context.Deleteable<ViewTag>(x => x.ViewId == input.Id).ExecuteCommandAsync();
        await _rep.DeleteAsync(entity);   //假删除
    }

    /// <summary>
    /// 更新视图
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    public async Task Update(UpdateMainViewInput input)
    {
        var entity = input.Adapt<GenerateMainView>();
        await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
    }

    /// <summary>
    /// 获取视图
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    public async Task<GenerateMainView> Get([FromQuery] QueryByIdMainViewInput input)
    {
        return await _rep.GetFirstAsync(u => u.Id == input.id);
    }

    /// <summary>
    /// 获取视图列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "List")]
    [AllowAnonymous]
    public async Task<List<MainViewOutput>> List([FromQuery] MainViewInput input)
    {
        return await _rep.AsQueryable().Select<MainViewOutput>().ToListAsync();
    }




}

